﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Web.UI;
using Antu.WebControls;
using Antu.Core.Components;
using Antu.Core.Services;
using System.Web.UI.WebControls;
using EluZSProvider;
using System.Web;
using System.Data;

namespace WebSys.UI.Control.ZhengShou
{
    public class XF_ZDRYInfoControl : MPBasicTemplate
    {
        EluZSDataProvider _EluZSDataProvider = (EluZSDataProvider)ProviderService.ProviderServiceMange.GetProvider("EluZSProvider");
        public string userid = string.Empty;
        DropDownList ddl信访类别, DropDownList1;
        TextBox txtID, txtPNID, txtXFRYID;
        Label 项目名称, 项目地点, 征收期限, 许可证号, 征收人, 征收实施单位, 评估机构;
        MPPersistence Persistence1;
        Panel panel1;
        Button btn_Insert, btnUpload;
        protected override void OnLoad(EventArgs e)
        {
            if (HttpContext.Current.Session["UserID"] == null)
            {
                Antu.Utilities.UtilitiesPage.AlertDialogClear("登录超时，请重新登录！", "window.parent.location.href('../login.aspx');");
            }
            else
            {
                userid = HttpContext.Current.Session["UserID"].ToString();
            }
            base.OnLoad(e);
            InitControl();
            if (!base.Page.IsPostBack)
            {
                Persistence1.Table = "ZS_XINFANGZD";
                Persistence1.Key = "ID";
                Persistence1.KeyValue = HttpContext.Current.Request["id"];
                Persistence1.ConnStr = _EluZSDataProvider.GetConnectionString();
                QSDataDLL();
            }
            Persistence1.BindControl = panel1;
            btn_Insert.Click += new EventHandler(btn_Insert_Click);
            btnUpload.Click += new EventHandler(btnUpload_Click);
            IsLookPerson();
            if (Page.Request["ID"]==null)
            {
                btnUpload.Visible = false;
            }
        }

        private void IsLookPerson()
        {
            string sql = "Select rolename from zs_role where id in (Select roleid from zs_user_in_role where userid = '" + userid + "')";
            DataTable dt = _EluZSDataProvider.GetTable(sql);
            if (dt.Rows.Count > 0)
            {
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    if (dt.Rows[i]["rolename"].ToString().Equals("查看人员"))
                    {
                        Button btn_Insert = (Button)FindControl("btn_Insert");
                        btn_Insert.Visible = false;
                    }
                }
            }
        }

        protected void InitControl()
        {
            Persistence1 = (MPPersistence)FindControl("Persistence1");
            panel1 = (Panel)FindControl("panel1");
            btn_Insert = (Button)FindControl("btn_Insert");
            ddl信访类别 = (DropDownList)FindControl("ddl信访类别");
            DropDownList1 = (DropDownList)FindControl("DropDownList1");

            txtID = (TextBox)FindControl("txtID");
            txtPNID = (TextBox)FindControl("txtPNID");
            txtXFRYID = (TextBox)FindControl("txtXFRYID");
            项目名称 = (Label)FindControl("项目名称");
            项目地点 = (Label)FindControl("项目地点");

            征收期限 = (Label)FindControl("征收期限");
            许可证号 = (Label)FindControl("许可证号");
            征收人 = (Label)FindControl("征收人");
            征收实施单位 = (Label)FindControl("征收实施单位");
            评估机构 = (Label)FindControl("评估机构");

            btnUpload = (Button)FindControl("btnUpload");
        }
        protected override void OnPreRender(EventArgs e)
        {
            base.OnPreRender(e);
            string pnid = base.Page.Request["PNID"];
            if (!string.IsNullOrEmpty(pnid))
            {
                EluZSProvider.Project project = _EluZSDataProvider.GetProjectXX(pnid);
                项目名称.Text = project.项目名称;
                项目地点.Text = project.项目地址;
                征收实施单位.Text = project.征收实施单位;
                评估机构.Text = _EluZSDataProvider.GetString("select 已通过征收评估机构 from zs_prono where pid='" + pnid + "'"); 
                DropDownList1.SelectedValue = project.区属;
                征收期限.Text = _EluZSDataProvider.GetString("select 拆迁期限 from zs_prono where pid='" + pnid + "'"); 
                许可证号.Text = _EluZSDataProvider.GetString("select 征收许可证号 from zs_prono where pid='" + pnid + "'");
                征收人.Text = _EluZSDataProvider.GetString("select 实施单位联系人 from zs_prono where pid='" + pnid + "'"); 
            }
        }
        protected void btn_Insert_Click(object sender, EventArgs e)
        {
            Persistence1.Update();
            HttpContext.Current.Response.Redirect("XF_ZDRYInfo.aspx?ID=" + Persistence1.KeyValue + "&PNID=" + txtPNID.Text);
        }
        protected void QSDataDLL()
        {
            List<EluZSProvider.Params> _params = _EluZSDataProvider.GetParams("信访类别");
            EluZSProvider.Params item = new EluZSProvider.Params();
            _params.Insert(0, item);
            ddl信访类别.DataSource = _params;
            ddl信访类别.DataValueField = "keyname";
            ddl信访类别.DataTextField = "keyname";
            ddl信访类别.DataBind();

            DataTable dt = _EluZSDataProvider.GetParamYB("区属", GetSqlWhere());
            DropDownList1.DataSource = dt;
            DropDownList1.DataValueField = "keyname";
            DropDownList1.DataTextField = "keyname";
            DropDownList1.DataBind();
        }

        protected string GetSqlWhere()
        {
            string sqlWhere = string.Empty;
            if (_EluZSDataProvider.IsQZSBM(userid))
            {
                sqlWhere += @" and (Select tt.ROLENAME  From zs_USER_IN_ROLE t Join  zs_Role tt  On  t.roleid=tt.id
Where  t.userid Like '%" + userid + "%') Like '%'||keyname||'%' ";
            }
            return sqlWhere;
        }



        protected void btnUpload_Click(object sender, EventArgs e)
        {
            if (!string.IsNullOrEmpty(Persistence1.KeyValue))
            {
                string sql = "Select id from zs_project where id in (Select pid from zs_prono where id = '" + base.Page.Request["PNID"].Trim() + "')";
                string id = _EluZSDataProvider.GetString(sql);
                string strScript = string.Format("<script>OpenFileUpload('{0}','{1}','{2}');</script>",
                    Persistence1.KeyValue, base.Page.Request["ID"].Trim(), id);
                Page.ClientScript.RegisterStartupScript(this.GetType(), "opennew", strScript);
            }
        }

    }
}


